<template>
    <div>
        <el-form :inline="true" :model="searchParam" class="demo-form-inline">
            <el-form-item>
                <el-input
                    clearable
                    v-model="searchParam.name"
                    placeholder="名称"
                />
            </el-form-item>
            <el-form-item style="width: auto">
                <el-button type="primary" size="small" @click="search">查询</el-button>
                <el-button type="primary" size="small" @click="addView(null)">添加类型</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="data.commentReportTypes" border style="width: 100%" v-loading="data.loading"
                  element-loading-text="拼命加载中">
            <el-table-column prop="name" label="名称" align="center" width="150px"/>
            <el-table-column
                prop="createTime"
                label="创建时间"
                width="180"
                align="center"
            >
            </el-table-column>
            <el-table-column
                prop="updateTime"
                label="更新时间"
                width="180"
                align="center"
            >
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="230" align="center">
                <template #default="scope">
                    <el-button
                        class="button-space"
                        @click="addView(scope.row)"
                        type="success"
                        size="small"
                    >编辑
                    </el-button>
                    <el-popconfirm
                        title="你确定要删除该评论举报类型吗？"
                        confirm-button-text="是"
                        cancel-button-text="否"
                        @confirm="deleteCommentReportType(scope.row.uuid)"
                    >
                        <template #reference>
                            <el-button
                                class="button-space"
                                type="danger"
                                size="small"
                            >删除
                            </el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 添加或修改评论举报类型-->
        <el-dialog :title="data.title" v-model="data.commentReportTypeAddOrUpdateDisable">
            <el-form :model="commentReportTypeAddOrUpdateData" :rules="rules" ref="addFormRef">
                <el-form-item label="名称" label-width="130px" prop="name">
                    <el-input v-model="commentReportTypeAddOrUpdateData.name" autocomplete="off"/>
                </el-form-item>
                <el-form-item label-width="130px">
                    <el-button @click="data.commentReportTypeAddOrUpdateDisable = false">取 消</el-button>
                    <el-button type="primary" @click="addOrUpdateCommentReportType">确认</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import {reactive, onMounted, ref} from "vue";
import {list, add, update, deleted} from "@/api/commentReport/commentReportType";
import {ElMessage} from "element-plus";

export default {
    setup() {
        const data = reactive({
            commentReportTypes: [],
            commentReportTypeAddOrUpdateDisable: false,
            title: '',
            updateUuid: ''
        })
        const searchParam = reactive({
            name: ''
        })
        const commentReportTypeAddOrUpdateData = reactive({
            name: '',
        })
        const rules = reactive({
            name: [
                {
                    required: true,
                    message: "请输入评论举报类型名称",
                    trigger: "blur",
                },
            ]
        })
        const addFormRef = ref(null)
        /**
         *
         * 获取全部评论举报类型
         */
        const listCommentReportType = () => {
            list(searchParam).then(res => {
                data.commentReportTypes = res.data
            })
        }
        const addView = (row) => {
            data.commentReportTypeAddOrUpdateDisable = true
            if (row != null) {
                data.title = '编辑'
                commentReportTypeAddOrUpdateData.name = row.name
                data.updateUuid = row.uuid
            } else {
                data.title = '添加'
                reset()
            }
        }
        /**
         * 重置数据
         */
        const reset = () => {
            commentReportTypeAddOrUpdateData.name = ''
        }
        /**
         * 添加评论举报类型
         */
        const addOrUpdateCommentReportType = () => {
            addFormRef.value.validate((valid) => {
                if (!valid) {
                    return false
                }
                if (data.updateUuid) {
                    update(commentReportTypeAddOrUpdateData, data.updateUuid).then(() => {
                        ElMessage({
                            message: '修改成功',
                            type: 'success',
                        })
                        data.commentReportTypeAddOrUpdateDisable = false
                        reset()
                        listCommentReportType();
                    })
                } else {
                    add(commentReportTypeAddOrUpdateData).then(() => {
                        ElMessage({
                            message: '添加成功',
                            type: 'success',
                        })
                        data.commentReportTypeAddOrUpdateDisable = false
                        reset()
                        listCommentReportType();
                    })
                }
            })
        }
        /**
         * 搜索
         */
        const search = () => {
            listCommentReportType()
        }
        /**
         * 删除评论举报类型
         * @param uuid
         */
        const deleteCommentReportType = (uuid) => {
            deleted(uuid).then(() => {
                ElMessage({
                    message: '删除成功',
                    type: 'success',
                })
                listCommentReportType()
            })
        }
        onMounted(() => {
            listCommentReportType()
        })
        return {
            data,
            searchParam,
            commentReportTypeAddOrUpdateData,
            rules,
            addFormRef,
            addView,
            addOrUpdateCommentReportType,
            search,
            deleteCommentReportType
        }
    }
}
</script>